package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Bgjlb;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.GenericManager;

public class BgjlbForm extends BasePage implements Serializable {
    private GenericManager<Bgjlb, Long> bgjlbManager;
    private Bgjlb bgjlb = new Bgjlb();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setBgjlbManager(GenericManager<Bgjlb, Long> bgjlbManager) {
        this.bgjlbManager = bgjlbManager;
    }

    public Bgjlb getBgjlb() {
        return bgjlb;
    }

    public void setBgjlb(Bgjlb bgjlb) {
        this.bgjlb = bgjlb;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        bgjlbManager.remove(bgjlb.getId());
        addMessage("bgjlb.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            bgjlb = bgjlbManager.get(id);
        } else {
            bgjlb = new Bgjlb();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (bgjlb.getId() == null || bgjlb.getId() == 0);
        bgjlb.setJbqk(jbqk);
        // set username who act this operation, get the user name from httprequest
        bgjlb.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        bgjlb.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        bgjlb.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        bgjlbManager.save(bgjlb);

        String key = (isNew) ? "bgjlb.added" : "bgjlb.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            // set the id to the next page
            this.id = bgjlb.getId();
            // return the update confirmation page
            return "edit";
        }
    }
} 